package com.ruoyi.zsyh.controller.applet.raker;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 服务订单 -- 配件单
 */
@Controller
@RequestMapping("/applet/zsyh/zsyhServiceOrderApplyMoneyRaker")
public class ZsyhServiceOrderApplyMoneyRakerAppletController extends BaseController
{

    @Autowired
    private IZsyhServiceOrderApplymoneyService zsyhServiceOrderApplymoneyService;

    @Autowired
    private IZsyhUserRepairstoreService zsyhUserRepairstoreService;

    @Autowired
    private IZsyhServiceOrderLogService zsyhServiceOrderLogService;

    @Autowired
    private IZsyhServiceOrderService zsyhServiceOrderService;

    /**
     * 市县管理商 订单中心 申请里程费
     * @return
     * @throws Exception
     */
    @RepeatSubmit
    @Transactional
    @RequestMapping("/applyOrderApplyMoneyRaker")
    @ResponseBody
    public AjaxResult applyOrderApplyMoney(String orderId, String relationId, String userId, String applyMoney, String applyReason, String applyImages)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String rakerId = store.getId();//市县id

        ZsyhServiceOrder order = zsyhServiceOrderService.selectZsyhServiceOrderById(orderId);
        ZsyhServiceOrderApplymoney _applymoney = new ZsyhServiceOrderApplymoney();
        _applymoney.setOrderId(orderId);
        List<ZsyhServiceOrderApplymoney> applymoneyList = zsyhServiceOrderApplymoneyService.selectZsyhServiceOrderApplymoneyList(_applymoney);
        int size = applymoneyList.size() + 1;
        String applymoneyId = orderId + "_F";
        if(size < 10){
            applymoneyId += "00" + size;
        }else{
            applymoneyId += "0" + size;
        }

        ZsyhServiceOrderApplymoney applymoney = new ZsyhServiceOrderApplymoney();

        applymoney.setId(applymoneyId);
        applymoney.setOrderId(orderId);
        applymoney.setFactoryId(order.getFactoryId());
        applymoney.setRelationId(relationId);
        applymoney.setRakerId(rakerId);
        applymoney.setApplyMoney(new BigDecimal(applyMoney));
        applymoney.setApplyReason(applyReason);
        applymoney.setApplyImages(applyImages);
        applymoney.setApplyType("1");//申请类型 1远程费 2旧配件回寄物流费
        applymoney.setApplyState("1");
        zsyhServiceOrderApplymoneyService.insertZsyhServiceOrderApplymoney(applymoney);

        ZsyhServiceOrderLog log = new ZsyhServiceOrderLog();
        log.setId(IdWorker.getId());
        log.setOrderId(orderId);
        log.setLogType("4");//分类 1师傅操作 2厂家操作 3平台操作 4市县操作
        log.setLogContent("市县管理商申请里程费");
        zsyhServiceOrderLogService.insertZsyhServiceOrderLog(log);

        return success("成功");
    }

    /**
     * 市县管理商 订单中心 获取费用单列表
     * @return
     * @throws Exception
     */
    @RequestMapping("/getRakerApplyMoneyList")
    @ResponseBody
    public AjaxResult getRakerApplyMoneyList(String userId, String key, String applyState, String pageNum, String pageSize)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String rakerId = store.getId();//市县id

        ZsyhServiceOrderApplymoney applymoney = new ZsyhServiceOrderApplymoney();
        applymoney.setRakerId(rakerId);
        applymoney.setApplyState(applyState);
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        map.put("key", key);
        applymoney.setParams(map);
        List<ZsyhServiceOrderApplymoney> applymoneyList = zsyhServiceOrderApplymoneyService.selectZsyhServiceOrderApplymoneyList(applymoney);

        return success("成功", applymoneyList);
    }


}
